/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
    
/**
 *
 * @author rr
 */
public class Run {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception, FileNotFoundException {
       
        
        int problemNo = 4;
        int threadCount = 1;
        if(args.length == 1){
            problemNo = Integer.parseInt(args[0]);
        }
        
        if(args.length == 2){
            problemNo = Integer.parseInt(args[0]);
            threadCount = Integer.parseInt(args[1]);
        }
        
        long startTime = System.currentTimeMillis();
   
        File problemFile = new File("res/problemSets/Example"+problemNo+".txt");
        SetUp.getInstance().init(problemFile);
        SetUp.getInstance().setThreadCount(threadCount);
        
        if(threadCount == 1){
        //no multithreading needed
        Solver solver = new project1.Solver();
        solver.solve();
        printWorkSchedule(solver.getSolution());
        // ... do something ...
        long estimatedTime = System.currentTimeMillis() - startTime;
        printWorkScheduleToFile(solver.getSolution(), "solutions/Solution"+problemNo+".txt", estimatedTime);    
        } else{
        
        ThreadedSolver.getInstance().solve();
        printWorkSchedule(ThreadedSolver.getInstance().getSolution());
        // ... do something ...
        long estimatedTime = System.currentTimeMillis() - startTime;
       
        printWorkScheduleToFile(ThreadedSolver.getInstance().getSolution(), "solutions/SolutionThreades"+problemNo+".txt", estimatedTime);    
        }
        
        
    }
    
    
    
    public static void printWorkScheduleToFile(int[] solution, String filepath, long estimatedTime) throws FileNotFoundException, UnsupportedEncodingException{
        PrintWriter writer = new PrintWriter(filepath, "UTF-8");
        for(int i = 0; i < SetUp.getInstance().getNumberOfEmployees(); ++i){
            
            writer.print("Employee No"+i);
            
            for(int j = 0; j<SetUp.getInstance().getLengthOfSchedule(); ++j){
                String s;
                if(solution[(i*SetUp.getInstance().getLengthOfSchedule() + j)]!=-1)
                    s=SetUp.getInstance().getNames()[solution[(i*SetUp.getInstance().getLengthOfSchedule() + j)]];
                else
                    s="~";
                writer.print(" "+s+" ");
            }
            
            writer.println();
                
        }
        writer.println("Solution Finding took "+estimatedTime+" miliseconds");
        writer.close();
    }
    
    public static void printWorkSchedule(int[] solution){
        
        if(solution == null){
            System.out.println("no solution");
            return;
        }
        
        for(int i = 0; i < SetUp.getInstance().getNumberOfEmployees(); ++i){
            
            System.out.print("Employee No"+i);
            
            for(int j = 0; j<SetUp.getInstance().getLengthOfSchedule(); ++j){
                String s;
                if(solution[(i*SetUp.getInstance().getLengthOfSchedule() + j)]!=-1)
                    s=SetUp.getInstance().getNames()[solution[(i*SetUp.getInstance().getLengthOfSchedule() + j)]];
                else
                    s="~";
                System.out.print(" "+s+" ");
            }
            
            
            
            System.out.println();
                
        }
        
    }
}
